$(function(){

	// 添加商品
	$(document).on('click','.phone-add',function(){
		$tbody = $(this).parents('h2').next('div').find('tbody');
		$phone_add = $('.phone-window');
		$phone_add.find('.name').val('');
		$phone_add.find('.show-pic').html('');
		$phone_add.find('.productdate').val('');
		$phone_add.find('.info').val('');
		$phone_add.find('.count').val('');
		$phone_add.find('.type').val('');
		$phone_add.find('.price').val('');
		$phone_add.find('.discountprice').val('');
		$popback= $('#popback');

		$phone_add.show();
		$popback.show();

		$phone_add.unbind().on('click','.submit',function(e){
			e.preventDefault();			
			var name = $phone_add.find('.name').val();
			var img = $phone_add.find('img').first().attr('src');
			var productdate = $phone_add.find('.productdate').val();
			var info = $phone_add.find('.info').val();
			var count = $phone_add.find('.count').val();
			var type = $phone_add.find('.type').val();
			var price = $phone_add.find('.price').val();
			var discountprice = $phone_add.find('.discountprice').val();
			if($phone_add.find('.name').val()=='' || $phone_add.find('.productdate').val()==''
				|| $phone_add.find('.count').val()=='' || $phone_add.find('.type').val()=='' ||$phone_add.find('.price').val()==''
				|| $phone_add.find('.discountprice').val()==''){
				layer.msg('缺少参数');
				return;
			}
			var param = {
				name: name,
                img: img,
				productdate: productdate,
				info: info,
				count: count,
				type: type,
				price: price,
				discountprice: discountprice
			};
			$.ajax({
				url: '/phone/index.php/Admin/Index/phoneAdd',
				type: 'POST',
				dataType: 'json',
				data: param,
				success: function(data){
					console.log(data);
					if(data.code == 0){
						var html = template('phone_template',data);
						$tbody.prepend(html);
						layer.msg('添加成功',{icon: 1});
						//layer.close(index);
						$phone_add.hide();
						$popback.hide();
					}
				},
				error: function(xhr){
					console.log(xhr);
				}
			});
			
		});
		$popback.click(function(event) {
			$phone_add.hide();
			$popback.hide();
		});
	});


	// 删除书籍
	$(document).on('click','.phone-del',function(){
		var $tr = $(this).parents('tr');
		var id = $tr.attr('data-id');
		layer.confirm('确认删除？', {
		  	btn: ['是的','取消'] //按钮
		}, function(){
			var param = {
				id: id
			};
			$.ajax({
				url: '/phone/index.php/Admin/Index/phoneDelete',
				type: 'POST',
				dataType: 'json',
				data: param,
				success: function(data){
					console.log(data);
					if(data.code == 0){
						layer.msg('删除成功', {icon: 1});
						$tr.remove();
					}
				},
				error: function(xhr){
					console.log(xhr);
				}
			});
		  	
		}, function(){
		  	//layer.msg('取消');
		});
	});

	// 书籍信息修改
	$(document).on('click','.phone-edit',function(){
		var $tbody = $(this).parents('tbody');
		var $tr = $(this).parents('tr');
		var id = $tr.attr('data-id');
		var name = $tr.find('.name').html();
		var img = $tr.find('img').first().attr('src');
		var productdate = $tr.find('.productdate').html();
		var info = $tr.find('.info').html();
		var count = $tr.find('.count').html();
		var type = $tr.find('.type').html();
		var price = $tr.find('.price').html(); 
		var discountprice = $tr.find('.discountprice').html(); 

		var $phone = $('.phone-window');
		$phone.find('.name').val(name);
		$phone.find('.show-pic').html('');
		var html = template('img_template',{src: img});
		$phone.find('.show-pic').append(html);
		$phone.find('.productdate').val(productdate);
		$phone.find('.info').val(info);
		$phone.find('.count').val(count);
		$phone.find('.type').val(type);
		$phone.find('.price').val(price);
		$phone.find('.discountprice').val(discountprice);
		
		var $popback = $('#popback');

		$phone.show();
		$popback.show();

		$phone.unbind().on('click','.submit',function(e){
			e.preventDefault();
			var phone = $phone.find('.name').val();
			var img = $phone.find('img').first().attr('src');
			var productdate = $phone.find('.productdate').val();
			var info = $phone.find('.info').val();
			var count = $phone.find('.count').val();
			var type = $phone.find('.type').val();
			var price = $phone.find('.price').val();
			var discountprice = $phone.find('.discountprice').val();
			if($phone.find('.name').val()=='' || $phone.find('.productdate').val()==''
				|| $phone.find('.count').val()=='' || $phone.find('.type').val()=='' ||$phone.find('.price').val()==''
				||$phone.find('.discountprice').val()==''){
				layer.msg('缺少参数');
				return;
			}
			
			var param = {
				id: id,
				name: name,
                img: img,
				productdate: productdate,
				info: info,
				count: count,
				type: type,
				price: price,
				discountprice: discountprice
			};

			$.ajax({
				url: '/phone/index.php/Admin/Index/phoneEdit',
				type: 'POST',
				dataType: 'json',
				data: param,
				success: function(data){
					console.log(data);
					if(data.code == 0){
						var html = template('phone_template',data);
						$tr.remove();
						$tbody.prepend(html);
						layer.msg('修改成功',{icon: 1});
						$phone.hide();
						$popback.hide();
					}
				},
				error: function(xhr){
					console.log(xhr);
				}
			});
			
		});

		$popback.click(function(event) {
			$phone.hide();
			$popback.hide();
			$phone.find('.show-pic').html('');
		});
	});

	// 图片上传
	var editor = UE.getEditor('myeditor');
	editor.ready(function () {
	    //editor.setDisabled();
	    editor.hide();
	    editor.addListener('beforeInsertImage', function (t, arg) {
	        //$("#path").val(arg[0].src);
	        //$("#preview").attr("src", arg[0].src);
	        //console.log(arg);
	        
	        for(var i=0;i<arg.length;i++){
	        	var html = template('img_template',arg[i]);
	        	$('.show-pic').append(html);
	        }
	    });
	});
	$('.add-pic').click(function(){
	    var myImage = editor.getDialog("insertimage");
	    myImage.open();
	});

	// 删除图片
	$('.show-pic').on('click','.del',function(){
		$(this).parents('.img').remove();
	});
});